const app = getApp()
const WXAPI = require("../../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '我要申诉', // 导航栏 中间的标题
      textColor: '#333333', // 标题颜色
      bgColor: '#ffffff', // 导航栏背景颜色
      isColor: true,
      isloading: false,
    },
    imgList: [],
    typels: [{
        ischoice: true,
        value: '已备案'
      },
      // {
      //   ischoice: false,
      //   value: '未备案'
      // }
    ],
    index: 0,
    shopName: '', //店铺名称
    isAnonymous: false, //是否匿名
    typeList:[],
    appealTypeLs: [], //申诉类型数组
    appealTypeValue:'',//申诉类型
    merchantId:'',//商家id
    codeImg:'',//验证码图片
    ruleFrom:{
      address:'',//地址
      areaId:'',//区域id，未备案商家填写
      businessId:'',//关联业务id
      code:'',//验证码
      contact:'',//联系方式
      content:'',//申诉内容
      img:'',//申诉图片Url
      imgs:[],//图片数组
      merchantId:'',//申述商家id
      merchantType:'1',//申诉商家类型 1 已备案商家 2未备案过
      name:'',//申诉人
      shopId:'',//申诉店铺id
      shopName:'',//申诉店铺名称
      title:'',//申诉标题
      type:'1',//申诉类型 gov_appeal_type
      uuId:'',//uuid
    },
    sumitLoading: false,// 是否提交状态中（防止重复点击）
    assetId:'',//
    id:'',//
    info:{},//商家信息
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.merchantId = options.merchantId
    this.data.ruleFrom.merchantId = options.merchantId
    this.data.ruleFrom.businessId = options.returnId
    this.data.assetId = options.assetId
    this.data.id = options.id
    this.getInfo()
    this.getdict()
    this.getShopName()
    this.getCaptchaImage()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  getInfo(){ //获取信息
    let data = {
      businessId: this.data.ruleFrom.businessId
    }
    WXAPI.applyInfo(data, (res) => {
      if (res.code == 200) {
        if (res.result.name!='') {
          this.data.ruleFrom.name = res.result.name
        }
        if (res.result.phone!='') {
          this.data.ruleFrom.contact = res.result.phone
        }
        console.log(this.data.ruleFrom)
        this.setData({
          info: res.result,
          ruleFrom: this.data.ruleFrom
        })
      }
    })
  },
  getCaptchaImage(){ //获取验证码 captchaImage
    let data = {}
    WXAPI.captchaImage(data, (res) => {
      if (res.code == 200) {
        this.data.codeImg = res.result.img
        this.data.ruleFrom.uuId = res.result.uuid
        this.setData({
          codeImg: this.data.codeImg
        })
      }
    })
  },
  getdict() { //类型字典
    var data = {
      dictType: 'gov_appeal_type ',
    }
    WXAPI.dict(data, (res) => {
      if (res.code == 200) {
        this.data.typeList = res.result[0].dictList
        this.data.appealTypeLs = res.result[0].dictList.map(x => {
          return x.value
        })
        this.setData({
          appealTypeValue: this.data.appealTypeLs[0],
          appealTypeLs: this.data.appealTypeLs
        })
      }
    })
  },
  getShopName() { //获取店铺名称 govMerchantList
    var data = {
      areaId: '',
      merchantName:'',
      merchantType:'',
    }
    WXAPI.govMerchantList(data, (res) => {
      if (res.code == 200) {
        let list = res.result
        list.map((item,index)=>{
          if (this.data.merchantId == item.id) {
            // console.log(item.additionalValue,index)
            this.data.shopName = item.additionalValue
            this.data.ruleFrom.shopName = item.additionalValue
          }
        })
        this.setData({
          shopName: this.data.shopName
        })
      }
    })
  },
  nameInput(e){ //获取申诉人输入值
    this.data.ruleFrom.name = e.detail.value
  },
  isAnonymousClick() { //点击匿名
    this.data.isAnonymous = !this.data.isAnonymous
    this.setData({
      isAnonymous: this.data.isAnonymous
    })
    if (this.data.isAnonymous) {
      this.setData({
        'ruleFrom.name': ''
      })
    }
  },
  phoneInput(e){ //联系方式输入值
    this.data.ruleFrom.contact = e.detail.value
  },
  bindPickerChange: function (e) { //申诉类型选择
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    let index = e.detail.value
    this.data.appealTypeValue = this.data.appealTypeLs[index]
    this.data.ruleFrom.type = this.data.typeList[index].id
    this.setData({
      appealTypeValue: this.data.appealTypeValue,
      index: e.detail.value
    })
  },
  titleInput(e){ //申诉标题
    this.data.ruleFrom.title = e.detail.value
  },
  contentInput(e){ //申诉正文
    this.data.ruleFrom.content = e.detail.value
  },
  chooseImg: function () { //图片选择
    var _this = this
    wx.chooseMedia({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // console.log(res)
        let uploaderList = res.tempFiles
        for (var i = 0; i < uploaderList.length; i++) {
          var data = {
            file: uploaderList[i].tempFilePath
          }
          WXAPI.appUploadFileNew(data, (res) => {
            // console.log("上传图片", res.result.url)
            if (res.code === 200) {
              // console.log(_this.data.imgList)
              _this.data.imgList.push(res.result.url)
              // console.log(_this.data.imgList)
              _this.setData({
                imgList: _this.data.imgList,
              })
            }
          })
        }
      }
    });
  },
  lookImg: function (e) { //预览图片
    var that = this;
    let imglists = that.data.imgList.map((item, index) => {
      return item
    })
    wx.previewImage({
      urls: imglists,
      current: that.data.imgList[e.currentTarget.dataset.index]
    })
  },
  cancelImg(e) { //删除图片
    let index = e.currentTarget.dataset.index
    // console.log(index, this.data.imgList)
    this.data.imgList.splice(index, 1);
    this.setData({
      imgList: this.data.imgList
    })
  },
  codeInput(e){ //验证码
    this.data.ruleFrom.code = e.detail.value
  },
  sumit(){ //提交
    var that = this
    if (!this.data.isAnonymous&&this.data.ruleFrom.name=='') {
      wx.showToast({
        icon:'none',
        title: '请输入您的称呼',
      })
      return
    }else if (this.data.ruleFrom.title=='') {
      wx.showToast({
        icon:'none',
        title: '请输入申诉标题',
      })
      return
    }
    else if (this.data.ruleFrom.code=='') {
      wx.showToast({
        icon:'none',
        title: '请输入验证码',
      })
      return
    }
    if (this.data.sumitLoading) { //避免重复提交
      console.log('防止重复')
      return
    }
    this.data.sumitLoading = true
    this.data.ruleFrom.imgs = this.data.imgList
    var data = {
      ...this.data.ruleFrom
    }
    wx.showLoading({
      title: '提交中',
      mask: true
    })
    WXAPI.addAppeal(data, (res) => {
      if (res.code == 200) {
        wx.hideLoading()
        wx.showToast({
          title: '提交成功',
        })
        setTimeout(()=>{
          wx.navigateTo({
            url: '../otherCarddetail?assetId=' + this.data.assetId + '&id=' + this.data.id
          })
        },500)
      }else if (res.message == '验证码已失效') {
        this.data.sumitLoading = false
        this.getCaptchaImage()
      }else{
        this.data.sumitLoading = false
        wx.hideLoading()
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  // onReachBottom() {

  // },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})